﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Salon.Reports;
using System.Data.SqlClient;

namespace Salon
{
    public partial class ReportSalesman : Form
    {
        public ReportSalesman()
        {
            InitializeComponent();
        }

        private void ReportSalesman_Load(object sender, EventArgs e)
        {
            this.cmbSalesman.DataSource = ServiceFactory.getUserService().getUsersToCombo();
            this.cmbSalesman.ValueMember = "Id";
            this.cmbSalesman.DisplayMember = "DictValue";
            this.cmbSalesman.ColumnName = "users.id";
            this.cmbSalon.Precondition = "users.salon=salon.id";
            this.cmbSalon.ColumnName = "users.salon";
            this.cmbSalon.Initialize();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            String from = "'" + dtpFrom.Value.ToShortDateString()+" 00:00:01'";
            String to = "'" + dtpTo.Value.ToShortDateString() + " 23:59:59'";
            String query = "SELECT users.name," +
                "users.surname," +
                "salon.name as salon," +
                "orders.id as order_id," +
                "orders.order_date as date," +
                "orders.cost," +
                "orders.price" +
                " FROM users, orders,salon WHERE users.id = orders.salesman_id AND orders.order_date between " + from + " AND " + to+" ";
                

            String condition = Global.getConditionString(Global.getFilterList(this));
            query += condition;

            DataSet ds = new DataSet("DSSalesmans");
            SqlDataAdapter da = new SqlDataAdapter(query, SqlService.getInstance().getConnection());
            da.TableMappings.Add("Table", "Salesmans");
            da.Fill(ds);

            RPTSalesmans report = new RPTSalesmans();

            report.SetDataSource(ds);
            report.SummaryInfo.ReportTitle = "Sprzedawcy";

            ReportViewer rv = new ReportViewer();
            rv.SetSource(report);
            rv.Show();
        }

      
    }
}
